package com.sina.shop.util;


import com.sina.shop.core.exception.ShopCustomException;
import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.ShopServerException;
import com.sina.shop.reflect.SinaReflectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 断言工具类
 * created  on 03/07/2017.
 */
public abstract class Assert extends org.springframework.util.Assert {

    private Assert() {

    }

    public static void notBlank(String s, String message) {
        if (StringUtils.isBlank(s)) {
            throw new ShopCustomException(ResultCode.dataValidError, message);
        }
    }

    /**
     * 等价于 {@link #assertNotNull(Object, Class DataException.class, ResultCode ResultCode.dataError, String, Object...)}
     */
    public static void assertNotNull(Object o, String message, Object... args) {
        assertNotNull(o, DataException.class, ResultCode.dataError, message, args);
    }


    public static void assertNotNull(Object o, Class<? extends ShopCustomException> exceptionClass
            , ResultCode resultCode, String messageFormat, Object... args)
            throws ShopServerException {
        if (o == null) {
            String msg = String.format(messageFormat == null ? "" : messageFormat, args);
            throw SinaReflectionUtils.newSinaException(exceptionClass, resultCode, msg
                    , new NullPointerException(msg));
        }
    }


    /**
     * 等价于 {@link #assertTrue(boolean, Class DataException.class, ResultCode ResultCode.dataError, String, Object...)}
     */
    public static void assertTrue(boolean value, String messageFormat, Object... args) {
        assertTrue(value, DataException.class, ResultCode.dataError, messageFormat, args);
    }

    public static void assertTrue(boolean value, ResultCode code, String messageFormat, Object... args) {
        assertTrue(value, DataException.class, code, messageFormat, args);
    }

    public static void assertTrue(boolean value, Class<? extends ShopCustomException> exceptionClass
            , ResultCode resultCode, String messageFormat, Object... args) {
        if (!value) {
            String msg = String.format(messageFormat == null ? "" : messageFormat, args);
            throw SinaReflectionUtils.newSinaException(exceptionClass, resultCode, msg, null);
        }
    }

    /**
     * 等价于 {@link #assertFalse(boolean, Class DataException.class, ResultCode ResultCode.dataError, String, Object...)}
     */
    public static void assertFalse(boolean value, String messageFormat, Object... args) {
        assertTrue(!value, DataException.class, ResultCode.dataError, messageFormat, args);
    }

    public static void assertFalse(boolean value, ResultCode code, String messageFormat, Object... args) {
        assertTrue(!value, DataException.class, code, messageFormat, args);
    }

    public static void assertFalse(boolean value, Class<? extends ShopCustomException> exceptionClass
            , ResultCode resultCode, String messageFormat, Object... args) {
        assertTrue(!value, exceptionClass, resultCode, messageFormat, args);
    }


}
